﻿// //------------------------------------------------------------------------------
// // <copyright file="" company="Dascoba Development">
// //  Copyright © Dascoba Development 2010
// // </copyright>
// //------------------------------------------------------------------------------
using System;

namespace Dascoba.Umb.FileManager.Dialogs
{
    using System.IO;
    using System.Linq;

    using Dascoba.Umb.FileManager.Support;

    using ICSharpCode.SharpZipLib.Zip;

    using umbraco;
    using umbraco.BasePages;

    /// <summary>
    ///   Extract Archive
    /// </summary>
    public partial class ExtractArchive : UmbracoEnsuredPage
    {
        /// <summary>
        ///   Handles the Load event of the Page control.
        /// </summary>
        /// <param name = "sender">The source of the event.</param>
        /// <param name = "e">The <see cref = "System.EventArgs" /> instance containing the event data.</param>
        protected void Page_Load(object sender, EventArgs e)
        {
            this.pane_extractarchive.Text = ui.Text(
                "fileman", "selectExtractLocation", this.Request["file"], CurrentUser);
        }

        /// <summary>
        ///   Handles the Click event of the 'Extract' button.
        /// </summary>
        /// <param name = "sender">The source of the event.</param>
        /// <param name = "e">The <see cref = "System.EventArgs" /> instance containing the event data.</param>
        protected void BtnExtractClick(object sender, EventArgs e)
        {
            bool extracted;
            var error = string.Empty;

            var fileUrl = this.Request["file"];
            var fileInfo = new FileInfo(Util.MapPath(fileUrl));
            var archiveArray = new[] { "zip", "gz", "gzip", "7z", "tar" };
            var extension = Util.GetSimpleExtension(fileInfo.Name);

            if (!archiveArray.Contains(extension))
            {
                extracted = false;
            }
            else
            {
                try
                {
                    var fastZip = new FastZip();
                    fastZip.ExtractZip(fileInfo.FullName, "", string.Empty);

                    extracted = true;
                }
                catch (Exception ex)
                {
                    error = ex.Message;
                    extracted = false;
                }
            }

            if (extracted)
            {
                this.litStatus.Text = ui.Text("fileman", "extractSuccess", fileInfo.Name, CurrentUser);
                this.imgStatus.ImageUrl = Constants.SuccessIcon;
            }
            else
            {
                this.pane_status.Text = ui.Text("fileman", "extractFailure");
                this.litStatus.Text = ui.Text("fileman", "error", error, CurrentUser);
                this.imgStatus.ImageUrl = Constants.FailureIcon;
            }

            this.pane_extractarchive.Visible = false;
            this.pane_status.Visible = true;
        }
    }
}